package com.example.mmlist2;

import android.content.Context;
import android.util.Base64;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

import com.example.mmlist2.storage.DatabaseHelper;
import com.example.mmlist2.storage.EncryptionUtils;
import com.example.mmlist2.storage.entity.Account;

import java.util.Arrays;
import java.util.List;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void addition_isCorrect2() {
        String passwordStr = "123";
        try {
            SecretKey secretKey = EncryptionUtils.generateSecretKey();
            passwordStr = EncryptionUtils.encrypt(passwordStr, secretKey);
            byte[] token = secretKey.getEncoded();
            System.out.println(Arrays.toString(token));
            String encoded = Base64.encodeToString(token, Base64.DEFAULT);
            System.out.println(encoded);

            SecretKey secretKey1 = new SecretKeySpec(secretKey.getEncoded(), "AES");
            passwordStr = EncryptionUtils.decrypt(passwordStr, secretKey1);
            System.out.println(passwordStr);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.example.mmlist2", appContext.getPackageName());
        try {
            DatabaseHelper db = new DatabaseHelper(appContext);
            Account account = new Account("wu", "admin", "edge浏览器",
                    "http://localhost", "123");
//            db.addAccount(account);
            List<Account> list = db.getAllAccounts();
            for (Account a : list) {
//                Log.d("db", a.toString());
                System.out.println(a.toString());
            }
            db.close();
        } catch (Exception e) {
            Log.e("db", "error");
        }
    }
}